﻿//Write a program to convert binary numbers to hexadecimal numbers (directly).


using System;
using System.Text;

public class BinaryToHexadecimal
{
    public static void Main()
    {
        Console.Write("Enter binary number to convert to hexadecimal\nNum = ");
        string number = Console.ReadLine().ToUpper();
        string[] str = PopulateArray(number);
        string[] result = Patern(str);
        Print(result);
    }

    private static string[] Patern(string[] array)
    {
        string[] binary = new string[] { "0000", "0001", "0010", "0011",
                                         "0100", "0101", "0110", "0111",
                                         "1000", "1001", "1010", "1011",
                                         "1100", "1101", "1110", "1111"};
        string[] hex = new string[] { "0", "1", "2", "3", "4", "5", "6", "7",
                                      "8", "9", "A", "B", "C", "D", "E", "F"};
        string[] result = new string[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            for (int j = 0; j < binary.Length; j++)
            {
                if (array[i] == binary[j])
                {
                    result[i] = hex[j];
                    break;
                }
            }
        }

        return result;
    }

    private static string[] PopulateArray(string str)
    {
        str = "000" + str;
        int lenght = str.Length / 4;
        string[] array = new string[lenght];
        int count = str.Length;
        for (int i = array.Length - 1; i >= 0; i--)
        {
            array[i] = str.Substring(count - 4, 4);
            count -= 4;
        }

        return array;
    }

    public static void Print(string[] array)
    {
        foreach (var item in array)
        {
            Console.Write(item);
        }
        Console.WriteLine();
    }
}